#include <interface\textField.h>
#include <interface\gl.h>
#include <interface\window.h>

#include <time.h>

        textField::textField(): abstractTextInput(),
                                TFfont( NULL ),
                                checkClick( -1 ),
                                lastCursorPos( 0 ),
                                rasterOffset( 0 )
{
    ELname = "<textField>";
    margin.left = 0;
    margin.top = 0;
    margin.bottom = 0;
    margin.right = 0;
}
        textField::textField(window* win):  abstractTextInput(win),
                                            TFfont( NULL ),
                                            checkClick( -1 ),
                                            lastCursorPos( 0 ),
                                            rasterOffset( 0 )
{
    ELname = "<textField>";
    margin.left = 0;
    margin.top = 0;
    margin.bottom = 0;
    margin.right = 0;
}
        textField::~textField(){
}

        string textField::getContent(){
    return content;
}

void textField::setFont(font* ft){
    TFfont = ft;
}
void textField::setMargin(int left,int bottom,int right,int top){
    margin.left = left;
    margin.right = right;
    margin.top = top;
    margin.bottom = bottom;
}

        void textField::display(){
    if( !styleS ) return;
    abstractTextInput::display();
    int pOffX = 0;
    int pOffY = 0;
    getOffset(&pOffX,&pOffY);
    glColor(bkgColFromSS(elm_textField));
    glDrawRect(enc,pOffX,pOffY);
    glColor(txtColFromSS(elm_textField));
    if( active && lastCursorPos >= (enc.right-enc.left-margin.left-margin.right) ){
        rasterOffset += (enc.right-enc.left-margin.left-margin.right) - lastCursorPos;
    }else if( active && lastCursorPos < (enc.right-enc.left-margin.left-margin.right-5) && rasterOffset != 0 ){
        rasterOffset += (enc.right-enc.left-margin.left-margin.right-5) - lastCursorPos;
        if( rasterOffset > 0 ) rasterOffset = 0;
    }
    glRasterPos(enc.left+margin.left+rasterOffset+pOffX,enc.bottom+1+margin.bottom+pOffY);
    if( active ){
        int cursorRasterPos = enc.left+margin.left+pOffX;
        if( checkClick == -1 ){
            glCurPrint(TFfont,cursor,&cursorRasterPos,content.c_str());
            if( (clock()/800)%2 ){
                if( parentWin && parentWin->getInput()->getInserMode() ){
                    glRasterPos(enc.left+margin.left-1+cursorRasterPos+pOffX,enc.bottom+2+margin.bottom+pOffY);
                    glPrint(TFfont,"|");
                }else{
                    glRasterPos(enc.left+margin.left+cursorRasterPos+pOffX,enc.bottom+1+margin.bottom+pOffY);
                    glPrint(TFfont,"_");
                }
            }
        }else{
            glGetCursorPrint(TFfont,checkClick,&cursor,&cursorRasterPos,content.c_str());
            checkClick = -1;
            if( (clock()/800)%2 ){
                if( parentWin && parentWin->getInput()->getInserMode() ){
                    glRasterPos(enc.left+margin.left-1+cursorRasterPos+pOffX,enc.bottom+2+margin.bottom+pOffY);
                    glPrint(TFfont,"|");
                }else{
                    glRasterPos(enc.left+margin.left+cursorRasterPos+pOffX,enc.bottom+1+margin.bottom+pOffY);
                    glPrint(TFfont,"_");
                }
            }
        }
        lastCursorPos = cursorRasterPos;
    }else{
        glPrint(TFfont,content.c_str());
    }
    glDisable(GL_SCISSOR_TEST);
}
       unsigned char textField::eventExec(unsigned char in){
    in = abstractTextInput::eventExec(in);
    return in;
}

void textField::mouseClickEvent(int){
    checkClick = parentWin->getInput()->getMouseX();
}
